package com.jsmy.marketing.fg;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jsmy.marketing.MPresenter;
import com.jsmy.marketing.MView;
import com.jsmy.marketing.utils.ToastUtil;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by PC_WLT on 2017/5/12.
 */

public abstract class BaseFragment extends Fragment implements MView {

    protected String TAG=this.getClass().getSimpleName();
    private Unbinder unbinder;
    public RxPermissions rxPermissions;
    public MPresenter mPresenter;
    protected Dialog mDialog;
    public static Fragment mInstance;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rxPermissions = new RxPermissions(getActivity());
        mPresenter=new MPresenter(this);
        mInstance=this;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        View v=inflater.inflate(resLayout(),container,false);
        unbinder=  ButterKnife.bind(this,v);
        return resLayout()==0?super.onCreateView(inflater, container, savedInstanceState):v;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    protected abstract int resLayout();

    protected abstract void initData();
    public abstract void update();
    /**
     * 通过new Intent(Context packageContext, Class<?> pClass)跳转到pClass页面
     *
     * @param pClass  ：要跳转的activity页面
     * @param pBundle :通过Bundle传递参数
     * @param uri     ：通过setData传递URI
     */
    protected void openActivity(Class<?> pClass, Bundle pBundle, Uri uri) {
        Intent intent = new Intent(getActivity(), pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        if (uri != null) {
            intent.setData(uri);
        }
        startActivity(intent);
    }

    protected void openActivity(Class<?> pClass, Bundle pBundle) {
        openActivity(pClass, pBundle, null);
    }

    protected void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }

    /**
     * 通过new Intent(String action)进行指定跳转
     *
     * @param pAction ：跳转的action动作名
     * @param pBundle ：要传递的参数
     * @param uri     ：要传递的URI(.getData())
     */
    protected void openActivity(String pAction, Bundle pBundle, Uri uri) {
        Intent intent = new Intent(pAction);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        if (uri != null) {
            intent.setData(uri);
        }
        startActivity(intent);
    }

    protected void openActivity(String pAction, Bundle pBundle) {
        openActivity(pAction, pBundle, null);
    }

    protected void openActivity(String pAction) {
        openActivity(pAction, null);
    }

    /**
     * 接收传递过来的Intent对象
     *
     * @return：Intent对象
     */
    protected Intent getPrevIntent() {
        return getPrevIntent();
    }

    /**
     * 直接接收Intent对象中的Bundle对象，getgetExtras();
     *
     * @return
     */
    protected Bundle getPrevExtras() {
        return getPrevIntent().getExtras();
    }


    /**
     * 直接传送Bundle
     * @param b
     * @param resultCode
     */
    protected void openActivityForResult(Bundle b, int resultCode){
        Intent intent = new Intent();
        if (b != null) {
            intent.putExtras(b);
        }
        startActivityForResult(intent,resultCode);
    }
    /**
     * 直接传送Bundle
     * @param resultCode
     */
    protected void openActivityForResult(Intent intent, int resultCode){
        startActivityForResult(intent,resultCode);
    }
    @Override
    public void showProgress() {
        if (mDialog == null) {
            mDialog = new Dialog(getContext());
        }
        if (mDialog != null && !mDialog.isShowing())
            mDialog.show();
    }

    @Override
    public void hideProgress() {
        if (mDialog != null)
            mDialog.dismiss();
    }
    @Override
    public void showLoadFailMsg(String msg) {
        ToastUtil.showLong(getContext(),msg);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder!=null){
            unbinder.unbind();
        }
    }
}
